package Final;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

public  class Select_2 {
    static JFrame jFrame = new JFrame("结果窗口");
    static ResultSet rs;
    static JTable jTable = new JTable();
public Select_2(){

     JLabel labID = new JLabel();
     JTextField textID1= new JTextField();
     JFrame jFrame = new JFrame("结果窗口");


        JFrame jf = new JFrame();
        jf.setTitle("表格组件");
        jf.setSize(500, 400);
        jf.setLocationRelativeTo(null);
        //jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
        Container c = jf.getContentPane();
    java.awt.Dimension dim2 = new java.awt.Dimension(50, 20);//标签的大小
    java.awt.Dimension dim3 = new java.awt.Dimension(400, 30);//输入框的大小
    java.awt.Dimension dim4 = new java.awt.Dimension(70, 40);//按钮的大小

    String[] columnNames={"ID","名字","性别","籍贯","班级","年龄"}; //列名

    Object[][] rowData=new Object[6][6]; //表格数据

    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();
    JPanel pan3 = new JPanel();

    JTabbedPane tabbedPane = new JTabbedPane();// 创建一个默认的选项卡面板
        JLabel tabLabelA = new JLabel();
        JLabel tabLabelB = new JLabel();
        JLabel tabLabelD = new JLabel();

        // 将标签组件添加到选项卡中

    JLabel jLabel = new JLabel("请输入");
    jLabel.setPreferredSize(dim2);
    pan1.add(jLabel);

    //添加“学号”标签

    labID.setText("学号");
    labID.setPreferredSize(dim2);
    pan1.add(labID);

    //添加账号输入框，并添加监控事件

    textID1.setPreferredSize(dim3);
    pan1.add(textID1);

    //添加一个删除按钮
    JButton button_delete = new JButton();
    button_delete.setText("查询");
    button_delete.setPreferredSize(dim4);
    button_delete.setBounds(10, 300, 300, 40);
    pan1.add(button_delete);

   //
    JLabel jLabel2 = new JLabel("请输入");
    jLabel2.setPreferredSize(dim2);
    pan2.add(jLabel2);

    //添加“学号”标签
    JLabel labName = new JLabel();
    labName.setText("名字");
    labName.setPreferredSize(dim2);
    pan2.add(labName);

    //添加账号输入框，并添加监控事件
    TextField textID2 = new TextField();
    textID2.setPreferredSize(dim3);
    pan2.add(textID2);

    //添加一个删除按钮
    JButton button_select = new JButton();
    button_select.setText("查询");
    button_select.setPreferredSize(dim4);
    button_select.setBounds(10, 300, 300, 40);
    pan2.add(button_select);

//
    JLabel jLabel3 = new JLabel("请输入");
    jLabel3.setPreferredSize(dim2);
    pan3.add(jLabel3);

    //添加“学号”标签
    JLabel jLabel1 = new JLabel();
    jLabel1.setText("籍贯");
    jLabel1.setPreferredSize(dim2);
    pan3.add(jLabel1);

    //添加账号输入框，并添加监控事件
    TextField textOigin = new TextField();
    textOigin.setPreferredSize(dim3);
    pan3.add(textOigin);

    //添加一个删除按钮
    JButton button_selectby = new JButton();
    button_selectby.setText("查询");
    button_selectby.setPreferredSize(dim4);
    button_selectby.setBounds(10, 300, 300, 40);
    pan3.add(button_selectby);


    tabbedPane.add("按id查看", pan1);
    tabbedPane.add("按名字查询",pan2);
    tabbedPane.add("按名籍贯查询",pan3);

        //tabbedPane.add(tabLabelA);

        c.add(tabbedPane);
        jf.setVisible(true);


    button_delete.addActionListener(e -> {
        Connection conn = Sql.open();



        try {

            String sql = "select * from Student where ID=?";

            PreparedStatement pstmt = null;
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,textID1.getText());
            rs = pstmt.executeQuery();
            int count = 0;
            int flag = 0;
            while(rs.next())
            {
                flag = 1;
                rowData[count][0]=rs.getString("ID"); //初始化数组内容
                rowData[count][1]=rs.getString("name");
                rowData[count][2]=rs.getString("sex");
                rowData[count][3]=rs.getString("Origin");
                rowData[count][4]=rs.getString("Class");
                rowData[count][5]=Integer.toString(rs.getInt("age"));

                count++;

            }
            if(flag == 1){
                jFrame.setSize(1000, 200);
                jTable=new JTable(rowData,columnNames); //实例化表格
                jTable.getColumn("年龄").setMaxWidth(5); //设置行宽
                jFrame.add(new JScrollPane(jTable),BorderLayout.CENTER); //增加组件
                jFrame.setVisible(true);
            }
            else{
                new SelectById.NoFound();
            }




        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Sql.close(conn);
        }

    });

    button_select.addActionListener(e -> {
        Connection conn = Sql.open();




        try {

            String sql = "select * from Student where name=?";

            PreparedStatement pstmt = null;
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,textID2.getText());
            rs = pstmt.executeQuery();
            //rs.next();
            int count = 0;
            int flag = 0;
            while(rs.next())
            {
                flag = 1;
                rowData[count][0]=rs.getString("ID"); //初始化数组内容
                rowData[count][1]=rs.getString("name");
                rowData[count][2]=rs.getString("sex");
                rowData[count][3]=rs.getString("Origin");
                rowData[count][4]=rs.getString("Class");
                rowData[count][5]=Integer.toString(rs.getInt("age"));

                count++;

            }
            if(flag == 1){
                //System.out.println(disp);
                //textField.setText(disp);
                //jLabel.setText(disp);
                jFrame.setSize(1000, 200);
                jTable=new JTable(rowData,columnNames); //实例化表格

                jTable.getColumn("年龄").setMaxWidth(5); //设置行宽
                jFrame.add(new JScrollPane(jTable),BorderLayout.CENTER); //增加组件
                jFrame.setVisible(true);
            }else {
                new SelectById.NoFound();
            }


            //Student s = new Student(ID,name,sex,Origin,Class,age);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Sql.close(conn);
        }

    });

    button_selectby.addActionListener(e -> {
        Connection conn = Sql.open();

        try {
            String sql = "select * from Student where Origin=?";
            PreparedStatement pstmt = null;
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,textOigin.getText());
            rs = pstmt.executeQuery();
            int flag = 0;
            int count = 0;
            while(rs.next())
            {

                flag = 1;
                rowData[count][0]=rs.getString("ID"); //初始化数组内容
                rowData[count][1]=rs.getString("name");
                rowData[count][2]=rs.getString("sex");
                rowData[count][3]=rs.getString("Origin");
                rowData[count][4]=rs.getString("Class");
                rowData[count][5]=Integer.toString(rs.getInt("age"));
                count++;

            }
            if(flag == 1){
                //System.out.println(disp);
                //textField.setText(disp);
                //jLabel.setText(disp);
                jFrame.setSize(1000, 200);
                jTable=new JTable(rowData,columnNames); //实例化表格

                jTable.getColumn("年龄").setMaxWidth(5); //设置行宽
                jFrame.add(new JScrollPane(jTable),BorderLayout.CENTER); //增加组件
                jFrame.setVisible(true);
            }else {
                new SelectById.NoFound();
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Sql.close(conn);
        }

    });

    }

    public static void main(String[] args) {
        new Select_2();
    }
}
